<template>
	<view class="icons" @click.stop="likeTap">
		<!-- 动态控制type用于处理是否收藏 -->
		<uni-icons size="22" color="#155915" :type="like?'heart-filled':'heart'"></uni-icons>
	</view>
</template>

<script>
	export default {
		props: {
			item: {
				type: Object,
				default () {
					return {}
				}
			}
		},
		name: "likes",
		data() {
			return {
				// 判断是空心还是实心 默认是空心
				like: false
			};
		},

		// 使用watch监听islike是否赋值成功
		watch: {
			item(newVal) {
				this.like = this.item.is_like
			}
		},
		created() {
			this.like = this.item.is_like
		},
		methods: {
			likeTap() {
				this.like = !this.like
				console.log("收藏成功");
				// 点击时调用
				this.setupdateLikes()
			},
			// 调用收藏 接口
			setupdateLikes() {
				this.$api.update_like({
					user_id: '628f404768cebc0001002da1',
					article_id: this.item._id
				}).then(res => {
					uni.hideLoading()
					uni.showToast({
						title: this.like ? '收藏成功' : '取消收藏',
						icon: 'none'
					})
					console.log(res);
				}).catch(()=>{
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>
	.icons {
		position: absolute;
		right: 0;
		top: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 20px;
		height: 20px;
		/* border: 1px red solid; */
	}
</style>
